package com.koron.order.common.service.impl;

import cn.hutool.core.date.DateUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.koron.bean.query.PageQuery;
import com.koron.order.common.bean.po.OrderAllPo;
import com.koron.order.common.bean.po.OrderEquipmentPo;
import com.koron.order.common.bean.query.OrderAllQuery;
import com.koron.order.common.mapper.OrderAllMapper;
import com.koron.order.common.mapper.OrderEquipmentMapper;
import com.koron.order.common.service.OrderEquipmentService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * (OrderEquipment)表服务实现类
 *
 * @author zhouj
 * @since 2021-11-09 18:50:33
 */
@Service("orderEquipmentService")
public class OrderEquipmentServiceImpl implements OrderEquipmentService {

    @Override
    @TaskAnnotation("queryById")
    public List<OrderEquipmentPo> queryById(SessionFactory factory,String id) {

        OrderEquipmentMapper mapper = factory.getMapper(OrderEquipmentMapper.class);

        return mapper.queryById(id);
    }

    @Override
    @TaskAnnotation("queryList")
    public List<OrderEquipmentPo> queryList(SessionFactory factory, OrderEquipmentPo orderEquipment) {

        OrderEquipmentMapper mapper = factory.getMapper(OrderEquipmentMapper.class);

        List<OrderEquipmentPo> list = mapper.queryList(orderEquipment);

        return list;
    }

    @Override
    @TaskAnnotation("maintenanceStat")
    public Map<String, Integer> maintenanceStat(SessionFactory factory, OrderEquipmentPo queryPo) {
        Assert.isTrue(queryPo != null && StringUtils.isNotBlank(queryPo.getEquipmentId()), "设备id不能为空");
        OrderEquipmentMapper mapper = factory.getMapper(OrderEquipmentMapper.class);
        OrderAllMapper orderMapper = factory.getMapper(OrderAllMapper.class);
        List<OrderEquipmentPo> orderEquipmentPos = mapper.queryList(new OrderEquipmentPo().setEquipmentId(queryPo.getEquipmentId()));
        // 设备的所有维修单id
        List<String> idList = orderEquipmentPos.stream().map(OrderEquipmentPo::getId).collect(Collectors.toList());
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.MONTH, 0); calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.set(Calendar.HOUR_OF_DAY, 0); calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);calendar.set(Calendar.MILLISECOND, 0);
        List<Date> createSection = new ArrayList<>();
        createSection.add(calendar.getTime());
        calendar.add(Calendar.YEAR, 1);
        createSection.add(calendar.getTime());
        Map<String, Integer> result = new LinkedHashMap<>();
        // 初始化12个月份统计数据为0
        Stream.iterate(0, i -> i + 1).limit(12).forEach(i -> result.put(String.format("%02d", (i + 1)), 0));
        if(CollectionUtils.isNotEmpty(idList)) {
            List<OrderAllPo> orderAllPos = orderMapper.queryList(new OrderAllQuery().setIdList(idList).setCreateSection(createSection));
            orderAllPos.stream().forEach(row -> {
                String month = DateUtil.format(row.getCreateTime(), "MM");
                result.put(month, result.get(month) + 1);
            });
        }
        return result;
    }

    @Override
    @TaskAnnotation("queryPageList")
    public PageInfo queryPageList(SessionFactory factory, OrderEquipmentPo orderEquipment, PageQuery pageQuery) {

        PageHelper.startPage(pageQuery.getPage(), pageQuery.getPageSize(), pageQuery.getOrderBy());
        OrderEquipmentMapper mapper = factory.getMapper(OrderEquipmentMapper.class);

        List<OrderEquipmentPo> list = mapper.queryList(orderEquipment);

        return new PageInfo(list);
    }

}